package net.roseindia;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

import user.User;

public class ManageEmployee {
	private static SessionFactory sf;
	private static ServiceRegistry serviceRegistry;

	public static void main(String[] args) {
		//TODO : gestion des thread local pour hibernate, pour le moemnt tout est monothread...
		SessionFactory sessionFactory = null;
		try {
			Configuration cfg = new Configuration().configure("hibernate.cfg.xml");   
			StandardServiceRegistryBuilder sb = new StandardServiceRegistryBuilder();
			sb.applySettings(cfg.getProperties());
			StandardServiceRegistry standardServiceRegistry = sb.build();           	
			 sessionFactory = cfg.buildSessionFactory(standardServiceRegistry);
		} catch (Throwable ex) {
			System.err.println("Failed to create sessionFactory object." + ex);
			throw new ExceptionInInitializerError(ex);
		}
		System.out.println("Hibernate One to Many Mapping Example Using XML ");
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		

 
        Employee e1 = new Employee();
        Employee e2 = new Employee();
        
        e1.setId(45);
        User user = new User("baba","baba","ho");
        /*
        user.setMail("freggg");
        user.setNbParisJustes(5);*/
 
        session.save(user);

 
        session.getTransaction().commit();
        session.close();
        sessionFactory.close();
	}
	
	public static void test() {
		SessionFactory sessionFactory = null;
		try {
			Configuration cfg = new Configuration().configure("hibernate.cfg.xml");   
			StandardServiceRegistryBuilder sb = new StandardServiceRegistryBuilder();
			sb.applySettings(cfg.getProperties());
			StandardServiceRegistry standardServiceRegistry = sb.build();           	
			 sessionFactory = cfg.buildSessionFactory(standardServiceRegistry);
		} catch (Throwable ex) {
			System.err.println("Failed to create sessionFactory object." + ex);
			throw new ExceptionInInitializerError(ex);
		}
		System.out.println("Hibernate One to Many Mapping Example Using XML ");
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		

 
		   Employee e1 = new Employee();
	        Employee e2 = new Employee();
	        
	        e1.setId(45);
	        User user = new User("babhra","baba","fgeget");
	        /*
	        user.setMail("freggg");
	        user.setNbParisJustes(5);*/
	 
	        session.save(user);

 
        session.getTransaction().commit();
        session.close();
        sessionFactory.close();
		
	}
	public static void test2() {
		System.out.println("BOUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU");
	}
	
}